package cn.fulong.frame.view;

import java.io.Serializable;

public class Pagination implements Serializable {
    public static int DEFAULT_PAGE_SIZE = 20; // 缺省每页条数

    private int rowCount; // 总条数
    private int pageSize; // 每页条数
    private int pageCount; // 总页数
    private int pageNo; // 当前页
    private int prePage; // 前一页
    private int nextPage; // 下一页
    private int rowFrom; // 当前页第一条
    private int rowTo; // 当前页最后一条

    // 根据总条数，当前页号，每页条数生成page
    public Pagination(int newRowCount, int newPageNo, int newPageSize) {
        this.setRowCount(newRowCount);

        if (newPageSize == 0) {
            this.setPageSize(DEFAULT_PAGE_SIZE);
        } else {
            this.setPageSize(newPageSize);
        }

        this.setPageCount((rowCount - 1) / pageSize + 1);

        if (newPageNo <= 0) {
            this.setPageNo(1);
        } else if (newPageNo > pageCount) {
            this.setPageNo(pageCount);
        } else {
            this.setPageNo(newPageNo);
        }

        if (pageNo == 1) {
            this.setPrePage(1);
        } else {
            this.setPrePage(pageNo - 1);
        }

        if (pageNo == pageCount) {
            this.setNextPage(pageNo);
        } else {
            this.setNextPage(pageNo + 1);
        }

        this.setRowFrom((pageNo - 1) * pageSize);

        this.setRowTo(pageNo * pageSize);

    }

    // 根据总条数，当前页号生成page，默认每页缺省条数
    public Pagination(int newRowCount, int newPageNo) {
        this(newRowCount, newPageNo, DEFAULT_PAGE_SIZE);
    }

    // 根据当前页号生成page，默认总条数为0
    public Pagination(int newPageNo) {
        this(0, newPageNo, DEFAULT_PAGE_SIZE);
    }

    public int getRowCount() {
        return rowCount;
    }

    public void setRowCount(int rowCount) {
        this.rowCount = rowCount;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getPageCount() {
        return pageCount;
    }

    public void setPageCount(int pageCount) {
        this.pageCount = pageCount;
    }

    public int getPageNo() {
        return pageNo;
    }

    public void setPageNo(int pageNo) {
        this.pageNo = pageNo;
    }

    public int getPrePage() {
        return prePage;
    }

    public void setPrePage(int prePage) {
        this.prePage = prePage;
    }

    public int getNextPage() {
        return nextPage;
    }

    public void setNextPage(int nextPage) {
        this.nextPage = nextPage;
    }

    public int getRowFrom() {
        return rowFrom;
    }

    public void setRowFrom(int rowFrom) {
        this.rowFrom = rowFrom;
    }

    public int getRowTo() {
        return rowTo;
    }

    public void setRowTo(int rowTo) {
        this.rowTo = rowTo;
    }
}
